package com.birdboot.controller;

import com.birdboot.annotations.Controller;
import com.birdboot.annotations.RequestMapping;
import com.birdboot.http.HttpServletRequest;
import com.birdboot.http.HttpServletResponse;
import com.birdboot.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Controller
public class ArticleController {

    @RequestMapping("/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response){
        /*
            1.获取前端传递过来的数据
            2.数据合法性校验[此处暂不校验]
            3.校验用户的登录状态[此处暂不校验]
            4.数据库操作
         */
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        try (Connection connection = DBUtil.getConnection();){
            String sql = "INSERT INTO article(title,author,content) VALUES(?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            //设置 ?
            ps.setString(1, title);
            ps.setString(2, author);
            ps.setString(3, content);
            //执行
            int i = ps.executeUpdate();
            if (i > 0){
                //发表成功
                response.sendRedirect("/article_success.html");
            }else{
                //发表失败
                response.sendRedirect("/article_fail.html");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}









